package com.axend.aerosense.dev.viewmodel;

import android.app.Activity;

import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.bean.BaseBean;
import com.axend.aerosense.base.bean.BaseStringBean;
import com.axend.aerosense.base.impl.IBaseView;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.viewmodel.CustomBaseViewModel;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.bean.RadarBean;
import com.axend.aerosense.common.bean.RadarType;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.dev.R;
import com.axend.aerosense.dev.entity.InstallationConfigurationBean;
import com.axend.aerosense.dev.entity.RadarInstallBean;
import com.axend.aerosense.dev.entity.RadarNetworkBean;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.ProgressDialogCallBack;
import com.axend.aerosense.network.callback.SimpleCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;

import io.reactivex.disposables.Disposable;

public class DevWavveProConfigViewModel extends CustomBaseViewModel<BaseBean> {
    private Disposable disposable;
    private RadarBean radarBean;
    /**
     * 安装类型
     * 0：侧装， 1：顶装
     */
    public MutableLiveData<Integer> installType = new MutableLiveData<>(0);
    /**
     * 角度
     * 0：角度1， 1：角度2
     */
    public MutableLiveData<Integer> angleType = new MutableLiveData<>(0);
    public MutableLiveData<String> installHeight = new MutableLiveData<>("0");
    public MutableLiveData<String> distanceToWall = new MutableLiveData<>("0");
    // 是否是WAVVE 系列
    public MutableLiveData<Boolean> isWavveSer = new MutableLiveData<>(false);
    public MutableLiveData<String> bedLength = new MutableLiveData<>("0");
    public MutableLiveData<String> bedWidth = new MutableLiveData<>("0");
    public MutableLiveData<String> bedHeight = new MutableLiveData<>("0");


    public DevWavveProConfigViewModel(IBaseView baseViewImpl, ILoadDataResult<BaseBean> loadDataResultImpl) {
        super(baseViewImpl, loadDataResultImpl);
    }

    public void setRadar(RadarBean bean){
        radarBean = bean;
        if(bean.getType() == RadarType.WAVVE_PRO) {
            isWavveSer.setValue(true);

        }else if(bean.getType() == RadarType.ASSURE_LITE) {
            isWavveSer.setValue(false);
        }
    }

    private int getNumber(String value){
        if(StringUtils.isEmpty(value)){
            return 0;
        }
        value = value.trim();
        if(StringUtils.equals(value, "0")){
            return 0;
        }

        try{
            return (int)Double.parseDouble(value);
        }catch (Exception e){
           return 0;
        }
    }

    public void setSettings(Activity activity){
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        int h = getNumber(installHeight.getValue());
        int w = getNumber(distanceToWall.getValue());
        int bl = getNumber(bedLength.getValue());
        int bw = getNumber(bedWidth.getValue());
        int bh = getNumber(bedHeight.getValue());
        installHeight.setValue("" + h);
        distanceToWall.setValue("" + w);
        bedLength.setValue("" + bl);
        bedWidth.setValue("" + bw);
        bedHeight.setValue("" + bh);

        RadarInstallBean radarInstallBean = new RadarInstallBean(
                radarBean.getRadarUuid(),
                installType.getValue(),
                h,
                w,
                installType.getValue() == 0 ? 0: angleType.getValue(),
                bl,
                bw,
                bh
        );

        disposable = EasyHttp.post(UrlApi.URL.SetInstallConfiguration.getUrl())
                .cacheMode(CacheMode.NO_CACHE)
                .headers("token", token)
                .upJson(JsonUtil.toJson(radarInstallBean))
                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onTokenExpire() {
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        e.printStackTrace();
                        ToastUtils.showLong(e.getMessage());
                    }

                    @Override
                    public void onSuccess(BaseStringBean bean) {
                        dismissProgress();
                        ToastUtils.showLong(R.string.common_success);
                    }
                });
    }

    public void onLoad(){
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        disposable = EasyHttp.post(UrlApi.URL.GetInstallConfiguration.getUrl())
                .cacheMode(CacheMode.NO_CACHE)
                .headers("token", token)
                .upJson(JsonUtil.toJson(new RadarNetworkBean(radarBean.getRadarUuid())))
                .execute(new SimpleCallBack<InstallationConfigurationBean>() {
                    @Override
                    public void onTokenExpire() {
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        loadDataFail(e.getMessage());
                    }

                    @Override
                    public void onSuccess(InstallationConfigurationBean configurationBean) {
                        RadarInstallBean bean = configurationBean.installationConfigurationInfo;
                        if(null != bean){
                            installType.setValue(bean.getRadarInstallationType());
                            bedLength.setValue("" + bean.getBedLength());
                            bedHeight.setValue("" + bean.getBedHeight());
                            bedWidth.setValue("" + bean.getBedWidth());
                            installHeight.setValue("" + bean.getInstallationHeight());
                            distanceToWall.setValue("" + bean.getDistanceToWall());
                            angleType.setValue(bean.getRadarBracketAngle());
                        }
                        loadDataSuccess(bean);
                    }
                });
    }



}
